<?php

class DefaultController extends Controller
{
        public $layout = 'application.modules.xyhadm.views.layouts.main';

        public $defaultAction='login'; 
    
         public function actions()
        { 
                return array( 
                        // captcha action renders the CAPTCHA image displayed on the contact page
                        'captcha'=>array(
                                'class'=>'CCaptchaAction',
                                'backColor'=>0xFFFFFF, 
                                'maxLength'=>'4',       // 最多生成几个字符
                                 'minLength'=>'2',       // 最少生成几个字符
                                 'height'=>'40'
                        ), 
                ); 

         }
      
      public function actionLogin()
    {
        //p($_POST);
              
                $this->layout = 'application.modules.xyhadm.views.layouts.login';
        if (Yii::app()->user->isGuest) {        
                     $model=new LoginForm();
                            // if it is ajax validation request
                    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
                    {
                       echo CActiveForm::validate($model);
                        Yii::app()->end();
                    }

                    // collect user input data
                    if(isset($_POST['LoginForm']))
                    {					  
                        $_POST['LoginForm']['password'] = md5($_POST['LoginForm']['password']);                      
                        $model->attributes=$_POST['LoginForm'];
                        //验证码
                        if($this->createAction('captcha')->validate($model->verifyCode, false)){ 
                                 if($model->login()){
                                    //Yii::app()->user->setFlash('login_admin_info',"登录成功); 
                                      // Yii::app()->jump->success("添加成功",yii::app()->createUrl('xyhadm/activity/index'));
                                      $this->redirect('/xyhadm/activity');
                                 }
                                 else {
                                     $ErrorMessage = BootExtend::GetErrorMassage($model);
                                       Yii::app()->jump->error('登陆失败! 用户名或密码错误！'.$ErrorMessage.'');
                                }
                            
                        }else {
                              $model->addError('verifyCode','验证码不对');  
                        }  
                       
                    }
             $this->render('login',array('model'=>$model));  
        }else{
             $this->redirect('/xyhadm/activity');
        }
        
   
       
    }
    
    
     public function actionAdminLogin()
    {
        //p($_POST);

        $this->layout = 'application.modules.xyhadm.views.layouts.login';
       
        if (Yii::app()->user->isGuest) {  
                     $model=new LoginForm();
                            // if it is ajax validation request
                    if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
                    {
                       echo CActiveForm::validate($model);
                        Yii::app()->end();
                    }
                    // collect user input data
                    if(isset($_POST['LoginForm']))
                    {					  
                        $_POST['LoginForm']['password'] = md5($_POST['LoginForm']['password']);
                        
                        $model->attributes=$_POST['LoginForm'];
                            // validate user input and redirect to the previous page if valid
                        //验证码
                        if($this->createAction('captcha')->validate($model->verifyCode, false)){ 
                                 if($model->Adminlogin()){
                                    //Yii::app()->user->setFlash('login_admin_info',"登录成功); 
                                     // Yii::app()->jump->success("添加成功",yii::app()->createUrl('xyhadm/activity/index'));
                                     $this->redirect('/xyhadm/activity');
                                 }
                                 else {
                                    
                                      $ErrorMessage = BootExtend::GetErrorMassage($model);
                                       Yii::app()->jump->error('登陆失败'.$ErrorMessage.'');
                                      $this->refresh();
                                }  
                        }else {
                              $model->addError('verifyCode','验证码不对');  
                        }  
                    }
             $this->render('login',array('model'=>$model));  
        }//已登录
        else{
            $this->redirect('/xyhadm/Activity');
        }
        // display the login form
    }
    
    
    
    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect('login');
    }
        
    
    public function actionInfo(){
        
        $id=Yii::app()->user->Alumni_id;    
        if($id){
            $sqlInfo = "SELECT a.id,au.username,a.rename,a.ctime,
           (SELECT name FROM xyh_area WHERE id=a.city_id) as city_name
            FROM xyh_alumni as a
            LEFT JOIN xyh_alumni_user as au on a.id = au.alumni_id
            LEFT JOIN xyh_school as s on a.school_id = s.id
            WHERE a.status=1 and a.is_del=2 and a.id={$id}";
        }else{ 
            $sqlInfo = "SELECT * FROM xyh_admin_user as ad WHERE ad.status=1 and ad.id={$id}";         
        }
          
        $dataInfo = Yii::app()->db->createCommand($sqlInfo)->queryRow();
        $this->render('info',array('info'=>$dataInfo));
    }
        
        
        
        
}